import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mmkv/mmkv.dart';
import 'package:seedfour/app/routes/app_routes.dart';
import 'package:seedfour/app/views/transfer/logic.dart';
import 'package:seedfour/app/views/withdraw/logic.dart';

import '../../../services/api_service.dart';
import '../../../services/common_service.dart';
import 'state.dart';

class BalanceLogic extends GetxController
    with GetSingleTickerProviderStateMixin {
  final BalanceState state = BalanceState();

  final mmkv = MMKV.defaultMMKV();
  final apiService = Get.find<ApiService>();
  final commonService = Get.find<CommonService>();

  late TabController tabController;
  late ScrollController scrollController;

  late String role;

  final _pageSize = 20;
  int _page = 1;
  int _type = 0;

  @override
  void onInit() {
    super.onInit();
    role = commonService.getRole();

    tabController = TabController(length: state.tabTitles.length, vsync: this);
    tabController.addListener(() {
      if (tabController.index.toDouble() == tabController.animation?.value) {
        _type = tabController.index;
        reload();
      }
    });

    scrollController = ScrollController();
    scrollController.addListener(() {
      if (scrollController.position.pixels >=
          scrollController.position.maxScrollExtent) {
        _getAccountInfo();
      }
    });

    reload();
  }

  void reload() {
    _page = 1;
    state.balanceList.clear();
    state.isLoading.value = true;
    _getAccountInfo();
  }

  void onClickAllList() {
    Get.toNamed(RouteGet.balanceRecord);
  }

  /// 点击提现
  void onClickWithdraw() async {
    var result = await Get.toNamed(RouteGet.withdraw, arguments:  WithdrawParams(balance: ''));

    if (result != null && result as bool) {
      reload();
    }
  }

  /// 点击转账
  void onClickTransfer() async{
    var result = await Get.toNamed(RouteGet.transfer, arguments: TransferParams(balance: state.balance.value));
    if (result != null && result as bool) {
      reload();
    }
  }

  void _getAccountInfo() {
    var action = role == 'me' ? 'flowList' : 'balanceList';
    var source = role == 'me' ? '' : 'app';

    apiService
        .getBalanceInfo(_page, _pageSize, action, role, _type, source)
        .then((account) => {
              state.isLoading.value = false,
              _page++,
              if (account != null)
                {
                  state.balance.value = account.balance?.sumAmount ?? '0.00',
                  state.userList.addAll(account.balance?.allocData ?? []),
                  state.balanceList.addAll(account.balance?.data ?? []),
                  if (account.balance!.data!.isEmpty)
                    {state.bottomTips.value = state.noMoreTip}
                  else
                    {state.bottomTips.value = state.loadingTip}
                }
              else
                {state.bottomTips.value = state.noMoreTip}
            });
  }

  // 点击记录
  void onClickRecord(int index) {
    Get.toNamed(RouteGet.balanceRecordDetail,
        arguments: state.balanceList[index]);
  }
}
